package com.nalai.market.v1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nalai.market.v1.mapper.*;
import com.nalai.market.v1.model.*;
import com.nalai.market.v1.service.IGoodsService;
import com.nalai.market.v1.vo.GoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com.nalai
 * @since 2021-08-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private LeveloneMapper leveloneMapper;
    @Autowired
    private LeveltwoMapper leveltwoMapper;

    public List<GoodsVo> findGoodsVoList() {
        List<GoodsVo> goodsVoList = new ArrayList<>();
        List<Goods> goodsList = goodsMapper.selectList(null);
        for (Goods goods : goodsList) {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(goods, goodsVo);
            Levelone levelone = leveloneMapper.selectById(goods.getCategoryLeveloneId());
            if (levelone != null) {
                goodsVo.setCategoryLevelone(levelone.getName());
            }
            Leveltwo leveltwo = leveltwoMapper.selectById(goods.getCategoryLeveltwoId());
            if (leveltwo != null) {
                goodsVo.setCategoryLeveltwo(leveltwo.getName());
            }
            User user = userMapper.selectById(goods.getUserId());
            if (user != null) {
                goodsVo.setNickname(user.getNickname());
            }
            School school = schoolMapper.selectById(goods.getSchoolId());
            if (school != null) {
                goodsVo.setSchoolName(school.getName());
            }
            goodsVoList.add(goodsVo);
        }
        goodsVoList.forEach(System.out::println);
        return goodsVoList;
    }

    @Override
    public GoodsVo findGoodsById(Integer id) {
        GoodsVo goodsVo = new GoodsVo();
        Goods goods = goodsMapper.selectById(id);
        if(goods != null){
            BeanUtils.copyProperties(goods, goodsVo);
            Levelone levelone = leveloneMapper.selectById(goods.getCategoryLeveloneId());
            if (levelone != null) {
                goodsVo.setCategoryLevelone(levelone.getName());
            }
            Leveltwo leveltwo = leveltwoMapper.selectById(goods.getCategoryLeveltwoId());
            if (leveltwo != null) {
                goodsVo.setCategoryLeveltwo(leveltwo.getName());
            }
            User user = userMapper.selectById(goods.getUserId());
            if (user != null) {
                goodsVo.setNickname(user.getNickname());
            }
            School school = schoolMapper.selectById(goods.getSchoolId());
            if (school != null) {
                goodsVo.setSchoolName(school.getName());
            }
        }
        return goodsVo;
    }
}
